// +build ignore

package main

import (
	"context"
	"fmt"
	"gbox-one-machine/conf"
	"gbox-one-machine/log"
	"gbox-one-machine/rpc"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"net/http"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func main() {

	cf := &conf.Config{
		Log: &conf.Log{
			Path: "./var/log",
		},
		App: &conf.App{
			Debug: true,
		},
	}

	log.InitLogger(cf)

	router := gin.Default()

	router.GET("/ws", upgrate)
	router.Run(":3454")
}

func upgrate(c *gin.Context) {
	ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"errorMsg": err.Error(),
		})
		return
	}

	r, err := rpc.NewJSONRPC(&rpc.JSONRPCOpts{
		Ws: ws,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"errorMsg": err.Error(),
		})
		return
	}

	r.Register("test", func(ctx context.Context, request interface{}) (interface{}, error) {
		return fmt.Sprintf("Recv data: %s", request), nil
	})
}
